﻿#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class MotorControlWidgetImpl : public Impl<MotorControlWidget>
        {

        public:
            void init();
            void exit();

        public:

            void setAntoMode(bool);
            void getValues(int flag = 0);
            void setValues(int flag = 0);
            void updateWidget();
            void setButtonSwtich(QLabel*, int);

        public:
            bool buttonDown(QObject* obj);
            bool buttonUp(QObject* obj);

        public:
            int _command = 0;
            int _status = 0;
            float _radium = 0.0;
            int _axisPosValue = 0;

        public:
            QTimer _timer;

        public:
            QGroupBox* _axisStatusGroup;
            QLabel* _axisAlarmStatus;
            QLabel* _axisCheckPosStatus;
            QLabel* _axisReadyPosStatus;
            QLineEdit* _axisPos;

            QGroupBox* _axisControlGroup;
            QPushButton* _autoControl;
            QPushButton* _forwardControl;
            QPushButton* _backwardControl;
            QPushButton* _zeroControl;
            QPushButton* _resetControl;
            QPushButton* _checkOverControl;
            QPushButton* _redLightControl;
            QPushButton* _greenLightControl;
            QPushButton* _axisMoveControl;
            QLineEdit* _curRadium;

            QGroupBox* _axisCalibGroup;
            Ctrl::TouchInput* _zeroDis;
            Ctrl::TouchInput* _objDis;
            Ctrl::TouchInput* _calibRadium;
            QPushButton* _calibControl;
        };

        void MotorControlWidgetImpl::init()
        {
            OI_Q(MotorControlWidget);

            q->setStyleSheet(QString("font-size:12pt}"));// 
            
            QGridLayout* mainLayout = new QGridLayout(q);
            mainLayout->setContentsMargins(0, 0, 0, 0);
            mainLayout->setSpacing(5);

            _axisControlGroup = new QGroupBox(q);
            _axisControlGroup->setTitle(MotorControlWidget::tr("轴控制栏"));

            QGridLayout* axisControlLayout = new QGridLayout(_axisControlGroup);
            _autoControl = new QPushButton(MotorControlWidget::tr("手动模式"));
            _autoControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _autoControl->installEventFilter(q);
            _autoControl->setCheckable(true);
            _autoControl->setChecked(true);

            _forwardControl = new QPushButton(MotorControlWidget::tr("向前"));
            _forwardControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _forwardControl->installEventFilter(q);

            _backwardControl = new QPushButton(MotorControlWidget::tr("向后"));
            _backwardControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _backwardControl->installEventFilter(q);

            _zeroControl = new QPushButton(MotorControlWidget::tr("回零"));
            _zeroControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _zeroControl->installEventFilter(q);

            _resetControl = new QPushButton(MotorControlWidget::tr("复位"));
            _resetControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _resetControl->installEventFilter(q);

            _checkOverControl = new QPushButton(MotorControlWidget::tr("Ready位"));
            _checkOverControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _checkOverControl->installEventFilter(q);

            _redLightControl = new QPushButton(MotorControlWidget::tr("红灯"));
            _redLightControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _redLightControl->installEventFilter(q);

            _greenLightControl = new QPushButton(MotorControlWidget::tr("绿灯"));
            _greenLightControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _greenLightControl->installEventFilter(q);

            _axisMoveControl = new QPushButton(MotorControlWidget::tr("检测位"));
            _axisMoveControl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            _axisMoveControl->installEventFilter(q);

            axisControlLayout->addWidget(_autoControl, 0, 0, 1, 2);
            axisControlLayout->addWidget(_forwardControl, 1, 0);
            axisControlLayout->addWidget(_backwardControl, 1, 1);
            axisControlLayout->addWidget(_zeroControl, 2, 0);
            axisControlLayout->addWidget(_resetControl, 2, 1);
            axisControlLayout->addWidget(_checkOverControl, 3, 0);
            axisControlLayout->addWidget(_axisMoveControl, 3, 1);
            axisControlLayout->addWidget(_redLightControl, 4, 0);
            axisControlLayout->addWidget(_greenLightControl, 4, 1);

            _axisStatusGroup = new QGroupBox(q);
            _axisStatusGroup->setTitle(MotorControlWidget::tr("轴状态栏"));
            QVBoxLayout* axisStatusLayout = new QVBoxLayout(_axisStatusGroup);

            QGridLayout* asTopLayout = new QGridLayout();
            _axisAlarmStatus = new QLabel(MotorControlWidget::tr("报警"));
            _axisAlarmStatus->setAlignment(Qt::AlignCenter);
            _axisCheckPosStatus = new QLabel(MotorControlWidget::tr("检测位"));
            _axisCheckPosStatus->setAlignment(Qt::AlignCenter);
            _axisReadyPosStatus = new QLabel(MotorControlWidget::tr("等待位"));
            _axisReadyPosStatus->setAlignment(Qt::AlignCenter);

            asTopLayout->addWidget(_axisCheckPosStatus, 0, 0);
            asTopLayout->addWidget(_axisReadyPosStatus, 0, 1);
            asTopLayout->addWidget(_axisAlarmStatus, 1, 0);

            QFormLayout* asBotLayout = new QFormLayout();
            _axisPos = new QLineEdit(q);
            _axisPos->setReadOnly(true);
            _curRadium = new QLineEdit(q);
            _curRadium->setReadOnly(true);
            asBotLayout->addRow(MotorControlWidget::tr("轴位置"), _axisPos);
            asBotLayout->addRow(MotorControlWidget::tr("当前规格半径"), _curRadium);

            axisStatusLayout->addLayout(asTopLayout);
            axisStatusLayout->addLayout(asBotLayout);

            _axisCalibGroup = new QGroupBox(q);
            _axisCalibGroup->setTitle(MotorControlWidget::tr("轴标定"));

            QFormLayout* axisCalibLayout = new QFormLayout(_axisCalibGroup);

            _zeroDis = new Ctrl::TouchInput(q);
            _zeroDis->setType(0);
            _zeroDis->setFlags(0);
            _zeroDis->setPrec(1);
            _zeroDis->setMaxValue(+999.0);
            _zeroDis->setMinValue(0);
            
            _objDis = new Ctrl::TouchInput(q);
            _objDis->setType(0);
            _objDis->setFlags(0);
            _objDis->setPrec(1);
            _objDis->setMaxValue(+999.0);
            _objDis->setMinValue(0);

            _calibRadium = new Ctrl::TouchInput(q);
            _calibRadium->setType(0);
            _calibRadium->setFlags(0);
            _calibRadium->setPrec(1);
            _calibRadium->setMaxValue(+999.0);
            _calibRadium->setMinValue(0);

            _calibControl = new QPushButton(MotorControlWidget::tr("标定"));
            _calibControl->installEventFilter(q);

            axisCalibLayout->addRow(MotorControlWidget::tr("原点至轮胎距离"), _zeroDis);
            axisCalibLayout->addRow(MotorControlWidget::tr("检测物距"), _objDis);
            axisCalibLayout->addRow(MotorControlWidget::tr("轮胎半径"), _calibRadium);
            axisCalibLayout->addWidget(_calibControl);

            mainLayout->addWidget(_axisControlGroup, 0, 0, 2, 1);
            mainLayout->addWidget(_axisStatusGroup, 0, 1, 1, 1);
            mainLayout->addWidget(_axisCalibGroup, 1, 1, 1, 1);

           // q->connect(_autoControl, &QPushButton::clicked, q, &MotorControlWidget::onManualButtonClicked);
            q->connect(&_timer, &QTimer::timeout, q, &MotorControlWidget::onTimerTimeout);
        }

        void MotorControlWidgetImpl::exit()
        {
            _timer.stop();
        }


        void MotorControlWidgetImpl::setAntoMode(bool enable)
        {
            if (enable)
            {
                _autoControl ->setText(MotorControlWidget::tr("自动模式"));
            }
            else
            {
                _autoControl->setText(MotorControlWidget::tr("手动模式"));
            }

            _autoControl->setChecked(!enable);

            _forwardControl->setEnabled(!enable);;
            _backwardControl->setEnabled(!enable);;
            _zeroControl->setEnabled(!enable);;
            _resetControl->setEnabled(!enable);;
            _checkOverControl->setEnabled(!enable);;
            _redLightControl->setEnabled(!enable);;
            _greenLightControl->setEnabled(!enable);;
            _axisMoveControl->setEnabled(!enable);;
            _calibControl->setEnabled(!enable);;
        }

        void MotorControlWidgetImpl::getValues(int flag)
        {
            OPCTask::instance()->readMotorPos(_axisPosValue);
            OPCTask::instance()->readControl(_command);
            OPCTask::instance()->readMotorStatus(_status);
        }

        void MotorControlWidgetImpl::setValues(int flag)
        {
            if (flag == 0)
            {
                OPCTask::instance()->writeControl(_command);
            }
            else if (flag == 1)
            {
                OPCTask::instance()->writeCalibrationMotor(_zeroDis->value().toDouble()
                    , _objDis->value().toDouble()
                    , _calibRadium->value().toDouble());

                OPCTask::instance()->writeControl(_command);
            }
        }

        void MotorControlWidgetImpl::updateWidget()
        {
            if (_status & MotorControlWidget::Axis_Error)
            {
                setButtonSwtich(_axisAlarmStatus, -1);
            }
            else
            {
                setButtonSwtich(_axisAlarmStatus, 0);
            }

            if(_status & MotorControlWidget::Axis_InspectPos)
            {
                setButtonSwtich(_axisCheckPosStatus, 1);
            }
            else
            {
                setButtonSwtich(_axisCheckPosStatus, 0);
            }

            if(_status & MotorControlWidget::Axis_WaitPos)
            {
                setButtonSwtich(_axisReadyPosStatus, 1);
            }
            else
            {
                setButtonSwtich(_axisReadyPosStatus, 0);
            }

            setAntoMode(_command & MotorControlWidget::Auto_Mode);

            _axisPos->setText(QString::number(_axisPosValue, 'f', 1));
            _curRadium->setText(QString::number(_radium, 'f', 1));
        }

        void MotorControlWidgetImpl::setButtonSwtich(QLabel* button, int flag)
        {
            if (flag == -1)
            {
                button->setStyleSheet("border:1px solid white;border-radius:5px;color:white;background-color:red;border");
            }
            else if (flag == 1)
            {
                button->setStyleSheet("border:1px solid white;border-radius:5px;color:black;background-color:green");
            }
            else
            {
                button->setStyleSheet(QString("border:1px solid white;border-radius:2px;"
                    "background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1,"
                    "stop:0 #f9fafd, stop:0.495 #f9fafd, stop:0.505 #d2d9e5, stop:1 #d2d9e5)"));

            }
        }

        bool MotorControlWidgetImpl::buttonDown(QObject* obj)
        {
            OI_Q(MotorControlWidget);

            if (obj == _autoControl)
            {
                return true;
            }
            else
            {
                if(_command & MotorControlWidget::Auto_Mode)
                {
                    return true;
                }

                else if (obj == _forwardControl)
                {
                    _command |=  MotorControlWidget::Forward_Jog;
                    setValues();
                    return true;
                }
                else if(obj == _backwardControl)
                {
                    _command |= MotorControlWidget::Backward_Jog;
                    setValues();
                    return true;
                }
                else if(obj == _zeroControl)
                {
                    _command |= MotorControlWidget::Axis_Home;
                    setValues();
                    return true;
                }
                else if(obj == _resetControl)
                {
                    _command |= MotorControlWidget::Axis_Reset;
                    setValues();
                    return true;
                }
                else if(obj == _checkOverControl)
                {
                    _command |= MotorControlWidget::Axis_InspectOver;
                    setValues();
                    return true;
                }
                else if(obj == _redLightControl)
                {
                    _command |= MotorControlWidget::Axis_RedLight;
                    setValues();
                    return true;
                }
                else if(obj == _greenLightControl)
                {
                    _command |= MotorControlWidget::Axis_GreenLight;
                    setValues();

                    return true;
                }
                else if(obj == _axisMoveControl)
                {
                    _command |= MotorControlWidget::Axis_StartMove;
                    setValues();

                    return true;
                }
                else if(obj == _calibControl)
                {
                    _command |= MotorControlWidget::Axis_Calib;
                    setValues(1);

                    return true;
                }
                else
                {
                    return false;
                }
             }
        }

        bool MotorControlWidgetImpl::buttonUp(QObject* obj)
        {
            OI_Q(MotorControlWidget);

            if(obj == _autoControl)
            {
                if(_command & MotorControlWidget::Auto_Mode)
                {
                    _command &= ( ~MotorControlWidget::Auto_Mode );
                }
                else
                {
                    _command |= MotorControlWidget::Auto_Mode;
                }

                setValues();

                return true;
            }
            else
            {
                if( _command & MotorControlWidget::Auto_Mode )
                {
                    return true;
                }

                else if(obj == _forwardControl)
                {
                    _command &= ( ~MotorControlWidget::Forward_Jog );
                    setValues();

                    return true;
                }
                else if(obj == _backwardControl)
                {
                    _command &= ( ~MotorControlWidget::Backward_Jog );
                    setValues();

                    return true;
                }
                else if(obj == _zeroControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_Home );
                    setValues();

                    return true;
                }
                else if(obj == _resetControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_Reset );
                    setValues();

                    return true;
                }
                else if(obj == _checkOverControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_InspectOver );
                    setValues();

                    return true;
                }
                else if(obj == _redLightControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_RedLight );
                    setValues();

                    return true;
                }
                else if(obj == _greenLightControl)
                {

                    _command &= ( ~MotorControlWidget::Axis_GreenLight );
                    setValues();

                    return true;
                }
                else if(obj == _axisMoveControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_StartMove );
                    setValues();

                    return true;
                }
                else if(obj == _calibControl)
                {
                    _command &= ( ~MotorControlWidget::Axis_Calib );
                    setValues();

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        MotorControlWidget::MotorControlWidget(QWidget * parent /*= nullptr*/) : QWidget(parent)
        {
            OI_I(MotorControlWidget)->init();
        }

        MotorControlWidget::~MotorControlWidget()
        {
            OI_E(MotorControlWidget)->exit();
        }

        void MotorControlWidget::onTimerTimeout()
        {
            OI_F(MotorControlWidget);
            d->getValues();
            d->updateWidget();
        }


        void MotorControlWidget::hideEvent(QHideEvent *event)
        {
            OI_IMPL()->_timer.stop();
        }

        void MotorControlWidget::showEvent(QShowEvent *event)
        {
            OI_IMPL()->_radium = ProductParams::params().radium;
            OI_IMPL()->_timer.start(100);
            onTimerTimeout();
        }

        bool MotorControlWidget::eventFilter(QObject *obj, QEvent *event)
        {
            OI_F(MotorControlWidget);
            if (event->type() == QEvent::MouseButtonPress)
            {
                return d->buttonDown(obj);
            }
            else if (event->type() == QEvent::MouseButtonRelease)
            {
                return d->buttonUp(obj);
            }
            else
            {
                return false;
            }
        }

    }
}